﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.PowerShell;
using System.Management.Automation;
using AutocompleteDemo;

namespace AutocompleteDemo.Cmdlet
{
    [Cmdlet(VerbsCommon.Set, "AutoCompletionServiceApplication", SupportsShouldProcess = true)]
    public sealed class SetAutoCompletionServiceApplication : SPSetCmdletBase<AutoCompletionServiceApplication>
    {
        // Fields
        private SPIisWebServiceApplicationPoolPipeBind m_appPoolPipeBind;
        private AutoCompletionServiceApplicationPipeBind m_pipeBind;
        private SPIisWebServiceApplicationPool m_resolvedApplicationPool;

        // Methods
        protected override void InternalValidate()
        {
            base.InternalValidate();
            if (this.Identity != null)
            {
                base.DataObject = this.Identity.Read();
                if (this.SPIisWebServiceApplicationPool != null)
                {
                    this.m_resolvedApplicationPool = this.SPIisWebServiceApplicationPool.Read();
                    if (null == this.m_resolvedApplicationPool)
                    {
                        base.WriteError(new InvalidOperationException("ApplicationPoolNotFound"), ErrorCategory.InvalidArgument, this);
                        base.SkipProcessCurrentRecord();
                    }
                }
            }
            else
            {
                base.SkipProcessCurrentRecord();
            }
        }

        protected override void UpdateDataObject()
        {
            if ((base.DataObject != null) && base.ShouldProcess(base.DataObject.Name))
            {
                base.DataObject.CloneValues(base.Fields);
                try
                {
                    if (this.m_resolvedApplicationPool != null)
                    {
                        base.DataObject.ApplicationPool = this.m_resolvedApplicationPool;
                        base.DataObject.CommitChanges();
                        base.DataObject.BeginProvision(null, null);
                    }
                    else
                    {
                        base.DataObject.CommitChanges();
                    }
                }
                catch (Exception ex)
                {
                    base.WriteError(ex, ErrorCategory.InvalidArgument, this);
                    base.SkipProcessCurrentRecord();
                }
            }
        }

        [Parameter(Mandatory = false, ValueFromPipeline = true, Position = 0)]
        public AutoCompletionServiceApplicationPipeBind Identity
        {
            get
            {
                return this.m_pipeBind;
            }
            set
            {
                this.m_pipeBind = value;
            }
        }

        [Parameter(Mandatory = false, ValueFromPipeline = true)]
        public SPIisWebServiceApplicationPoolPipeBind SPIisWebServiceApplicationPool
        {
            get
            {
                return this.m_appPoolPipeBind;
            }
            set
            {
                this.m_appPoolPipeBind = value;
            }
        }
    }


}
